<?php 
class Backstage_model extends CI_Model{
	function __construct(){
		parent::__construct();
		$this->load->database();
		$this->load->helper('url');
	}
	
	/* function get_admin_message($username){//pdo查询
		$dsn="mysql:dbname=admin;host=localhost"; 
		//$this->db->user()
		$db_user='root'; 
		$db_pass='905407339'; 
		try{ 
		$pdo=new PDO($dsn,$db_user,$db_pass); 
		$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);	//禁用模拟预处理语句,保证sql语句不被php解析
		}catch(PDOException $e){ 
		echo '数据库连接失败'.$e->getMessage(); 
		}
		$sql="select * from user where username='$username'";		
		$res=$pdo->query($sql);   
		return $res;
	} */
	function get_admin_message($username){//pdo查询
		$dsn="mysql:dbname=admin;host=localhost"; 
		//$this->db->user()
		$db_user='root'; 
		$db_pass='905407339'; 
		try{ 
		$pdo=new PDO($dsn,$db_user,$db_pass); 
		$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);	//禁用模拟预处理语句,保证sql语句不被php解析
		$pdo->exec("set names utf8");
		}catch(PDOException $e){ 
		echo '数据库连接失败'.$e->getMessage(); 
		}
		$sql="select * from user where username= ?";	
		$stmt = $pdo->prepare($sql);// 对 SQL 语句执行 prepare，得到 PDOStatement 对象 
		$exec = $stmt->execute(array($username));//array形式
		 if ($exec) {
		$res = $stmt->fetch(PDO::FETCH_ASSOC);
		} 
		return $res;	
	}
	

	//删除作品
	function del_pro(){
		if(!empty($_POST["del"])){
       foreach($_POST["del"] as $key=>$val){
			$array[] =$val;
       }
	}
		$array=implode(',',$array); 
		$cid=$this->uri->segment(3);
		$sql="delete from works where ID in ($array)";
		$quey=$this->db->query($sql);
		if($quey){ 
			$flag=true;
		}else{
			$flag=false;
		} 
		return $flag;
	}
	//添加作品
	function add_prod(){
		date_default_timezone_set('PRC'); //本地化时间，要不会报错的
		 $config['upload_path']= './upload/image';
                $config['allowed_types']= 'jpg|png|jpeg';
                $config['max_size']='10000';
				$config['file_name']=date('Y-m-d-H-i-s',time());//改文件名字为当前的时间
                $this->load->library('upload', $config);

                if ($this->upload->do_upload('pic'))
                {
					    $data = array('upload_data' => $this->upload->data());
						$full_path=$this->upload->data('full_path'); 
						$num=strpos($full_path,"upload");
						$path=substr($full_path,$num);
							$name=$this->input->post("name");
							$url=$this->input->post("url");
							$pic=$path;
							$data=array(
										'pic'=>$pic,
										'name'=>$name,
										'url'=>$url
										
										);
							$query=$this->db->insert("works",$data);
							if($query){
								redirect("Backstage/show_production");
							}else{
								echo "上传失败";
							}
                }
                else
                {
						$error = array('error' => $this->upload->display_errors());
						$this->load->view("backstage/up_false",$error);
						
                }
	}
	//获取作品信息
	function get_pro(){
		$id=$this->uri->segment(3);
		$query=$this->db->query("select * from works where id = '$id'");
		return $query->row_array();
	}
	//确认修改
	function check_edit_prod(){
		date_default_timezone_set('PRC'); //本地化时间，要不会报错的
		 $config['upload_path']= './upload/image';
                $config['allowed_types']= 'jpg|png|jpeg';
                $config['max_size']='10000';
				$config['file_name']=date('Y-m-d-H-i-s',time());//改文件名字为当前的时间
                $this->load->library('upload', $config);

                if ($this->upload->do_upload('pic'))
                {
					    $data = array('upload_data' => $this->upload->data());
						$full_path=$this->upload->data('full_path'); 
						$num=strpos($full_path,"upload");
						$path=substr($full_path,$num);
						$name=$this->input->post("name");
						$url=$this->input->post("url");
						$id=$this->input->post("id");
						$pic=$path;
						$data=array(
									'pic'=>$pic,
									'name'=>$name,
									'url'=>$url
									);
						$this->db->where("id",$id);
						if($this->db->update('works',$data)){
							redirect("Backstage/show_production");
						}else{
							echo "上传失败";
						}
                }
                else
                {
							$name=$this->input->post("name");
							$id=$this->input->post("id");
							$url=$this->input->post("url");
							$data=array(
										'name'=>$name,
										'url'=>$url
										);
							$this->db->where("id",$id);
							if($this->db->update('works',$data)){
								redirect("Backstage/show_production");
							}else{
								echo "上传失败";
							}
						
                }
			
	}
	//添加成员
	function add_team(){
		date_default_timezone_set('PRC'); //本地化时间，要不会报错的
				$config['upload_path']= './upload/image';
                $config['allowed_types']= 'jpg|png|jpeg';
                $config['max_size']='10000';
				$config['file_name']=date('Y-m-d-H-i-s',time());//改文件名字为当前的时间
                $this->load->library('upload', $config);
                if ($this->upload->do_upload('pic'))
                {
					    $data = array('upload_data' => $this->upload->data());
						$full_path=$this->upload->data('full_path'); 
						$num=strpos($full_path,"upload");
						$path=substr($full_path,$num);
							$name=$this->input->post("name");
							$grade=$this->input->post("grade");
							$time=$this->input->post("time");
							$motto=$this->input->post("motto");
							$direction=$this->input->post("direction");
							$qq=$this->input->post("qq");
							$mail=$this->input->post("mail");
							$pic=$path;
							$data=array(
										'pic'=>$pic,
										'name'=>$name,
										'direction'=>$direction,
										'time'=>$time,
										'motto'=>$motto,
										'grade'=>$grade,
										'qq'=>$qq,
										'mail'=>$mail
										);
							$query=$this->db->insert("team",$data);
							if($query){
								redirect("Backstage/show_partner");
							}else{
								echo "上传失败";
							}
                }
                else
                {
						$error = array('error' => $this->upload->display_errors());
						$this->load->view("backstage/up_false1",$error);
						
                }
	}
	//删除成员
	function del_part(){
		if(!empty($_POST["del"])){
       foreach($_POST["del"] as $key=>$val){
			$array[] =$val;
       }
	}
		$array=implode(',',$array); 
		$cid=$this->uri->segment(3);
		$sql="delete from team where ID in ($array)";
		$quey=$this->db->query($sql);
		if($quey){ 
			$flag=true;
		}else{
			$flag=false;
		} 
		return $flag;
	}
	//编辑成员
	function get_team(){
		$id=$this->uri->segment(3);
		$query=$this->db->query("select * from team where id = '$id'");
		return $query->row_array();
	}
	//确认修改
	function check_edit_team(){
		date_default_timezone_set('PRC'); //本地化时间，要不会报错的
		 $config['upload_path']= './upload/image';
                $config['allowed_types']= 'jpg|png|jpeg';
                $config['max_size']='10000';
				$config['file_name']=date('Y-m-d-H-i-s',time());//改文件名字为当前的时间
                $this->load->library('upload', $config);

                if ($this->upload->do_upload('pic'))
                {
					    $data = array('upload_data' => $this->upload->data());
						$full_path=$this->upload->data('full_path'); 
						$num=strpos($full_path,"upload");
						$path=substr($full_path,$num);
							$name=$this->input->post("name");
							$grade=$this->input->post("grade");
							$time=$this->input->post("time");
							$motto=$this->input->post("motto");
							$direction=$this->input->post("direction");
							$qq=$this->input->post("qq");
							$id=$this->input->post("id");
							$mail=$this->input->post("mail");
							$pic=$path;
							$data=array(
										'pic'=>$pic,
										'id'=>$id,
										'name'=>$name,
										'direction'=>$direction,
										'time'=>$time,
										'motto'=>$motto,
										'grade'=>$grade,
										'qq'=>$qq,
										'mail'=>$mail
										);
							$this->db->where("id",$id);
							if($this->db->update('team',$data)){
								redirect("Backstage/show_partner");
							}else{
								echo "上传失败";
							}
                }
                else
                {
						$name=$this->input->post("name");
						$grade=$this->input->post("grade");
						$time=$this->input->post("time");
						$motto=$this->input->post("motto");
						$direction=$this->input->post("direction");
						$qq=$this->input->post("qq");
						$id=$this->input->post("id");
						$mail=$this->input->post("mail");
						$pic=$path;
						$data=array(
									'id'=>$id,
									'name'=>$name,
									'direction'=>$direction,
									'time'=>$time,
									'motto'=>$motto,
									'grade'=>$grade,
									'qq'=>$qq,
									'mail'=>$mail
									);
						$this->db->where("id",$id);
						if($this->db->update('team',$data)){
							redirect("Backstage/show_partner");
						}else{
							echo "上传失败";
						}
						
                }
			
	}
	
	//关于我们
	function get_about(){
		$query=$this->db->query("select * from aboutus where id = '1'");
		return $query->row_array();
	}
	//修改关于我们
	function edit_about(){
		$id='1';
		$content=$this->input->post("content");
		$data=array(
					'content'=>$content
							);
					$this->db->where("id",$id);
					if($this->db->update('aboutus',$data)){
						$flag=true;
					}else{
						$flag=false;
					}
					return $flag;
	}
	
	//联系我们
	function get_contact(){
		$query=$this->db->query("select * from contact");
		return $query->result();
	}
	//删除联系我们
	function del_con(){
		if(!empty($_POST["del"])){
			foreach($_POST["del"] as $key=>$val){
			$array[] =$val;
			}
		}
		$array=implode(',',$array); 
		$cid=$this->uri->segment(3);
		$sql="delete from contact where ID in ($array)";
		$quey=$this->db->query($sql);
		if($quey){ 
			$flag=true;
		}else{
			$flag=false;
		} 
		return $flag;
	}
	//修改密码
	function update_admin($username,$new_password,$id){
		$new_password=md5($new_password);
		$data=array(
				'username'=>$username,
				'password'=>$new_password
		);
		$this->db->where("id",$id);
		if($this->db->update('user',$data)){
			$flag=true;
		}else{
			$flag=false;
		}
		return $flag;
	}
	function get_password($id){
		$sql="select * from user where id='$id'";
		$query=$this->db->query($sql);
		return $query->row_array();
	}
	//显示报名信息
	function show_enroll(){
			$sql="select * from enroll";
			$query=$this->db->query($sql);
			return $query->result();
	}
	function show_enroll_xinxi(){
			$id=$this->uri->segment(3);
			$sql="select * from enroll where id='$id'";
			$query=$this->db->query($sql);
			return $query->row_array();
	}
	//删除报名信息
	function del_enr(){
		if(!empty($_POST["del"])){
			foreach($_POST["del"] as $key=>$val){
			$array[] =$val;
			}
		}
		$array=implode(',',$array); 
		$cid=$this->uri->segment(3);
		$sql="delete from enroll where ID in ($array)";
		$quey=$this->db->query($sql);
		if($quey){ 
			$flag=true;
		}else{
			$flag=false;
		} 
		return $flag;
	}
	
	
	
	
	
	
	
	
}